<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<%@ taglib uri="/WEB-INF/tld/sys.tld" prefix="sys" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>重卡新车包牌业务比例配置</title>
    <jsp:include page="../../import.jsp" flush="true"/>
</head>
<body class="iframeBody">
<div class="Wrapper">
    <div>
        <div id="pageInTab" class="pageInTab">
            <sys:funcAuth resCd="bpCfgZkPage">
                <div tabid="tabA" title=" 重卡事业部总额度配置" id="tabAId" lselected="true">
                    <div class="fieldset1 clearFix">
                        <div class="fieldBox">
                            <div class="label">重卡本月包牌业务车辆占比:</div>
                            <div class="field">
                                <span id="bpPct">${bpPct}</span>%
                            </div>
                        </div>
                    </div>
                    <div class="fieldset1 clearFix">
                        <div class="fieldBox">
                            <div class="label">重卡包牌业务比例:</div>
                            <div class="field">
                                <span id="limitPct" style="color: red;">${limitPct}</span>%
                            </div>
                        </div>
                    </div>
                    <div class="fieldset1 clearFix">
                        <div class="fieldBox">
                            <div class="label">重卡当月免校验台数:</div>
                            <div class="field">
                                <span id="uncheckCntspn">${uncheckCnt}</span>
                            </div>
                        </div>
                        <sys:funcAuth resCd="bpCfgZkMod">
                            <div class="fieldBox">
                                <input name="uncheckCntVal" id="uncheckCntVal" type="input" value="" validate="{required:true}">
                                <input name="updateBtn" id="updateBtn" type="button" onclick="toUpdate()" value="确认修改"/>
                            </div>
                        </sys:funcAuth>
                    </div>
                    <div id="ligergridLog" class="sq-grid"></div>
                </div>
            </sys:funcAuth>
            <sys:funcAuth resCd="bpCfgProPage">
                <div tabid="tabB" title="省公司额度配置" id="tabBId" lselected="true">
                    <div id="ligergridPro" class="sq-grid"></div>
                </div>
            </sys:funcAuth>
            <sys:funcAuth resCd="bpCfgSubPage">
                <div tabid="tabC" title="分公司包牌业务比例展示" id="tabCId" lselected="true">
                    <div id="ligergridSub" class="sq-grid"></div>
                </div>
            </sys:funcAuth>
        </div>
    </div>
</div>

<script type="text/javascript">
    var toUpdate = function () {
        var count = Number($("#uncheckCntVal").val());
        if (count < 0 || count*10%10 != 0) {
            top.$.ligerDialog.SQalert({content: "请填写大于等于0的整数", SQ_type: "warning"});
            return false ;
        }
        $.ligerDialog.confirm("修改免校验台数为" + count, function (yes) {
            if (yes) {
                $.ajax({
                    type: 'get',
                    dataType: 'json',
                    async: false,
                    url: '<%=path%>/project/orgBpLimit/updateUnCheckCount.do?count=' + count,
                    success: function (result) {
                        if (result.code == 000000) {
                            initPageData();
                        } else {
                            top.$.ligerDialog.SQalert({content: "修改失败！", SQ_type: "warning"});
                        }
                    },
                    error: function () {
                        top.$.ligerDialog.SQalert({content: "修改失败！", SQ_type: "warning"});
                    }
                });
            }
        });

        function renderDate(rowdata, index, value) {
            var dateStr = "";
            if (null != value) {
                var date = new Date(value);
                dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
            }
            return dateStr;
        }
    }

    function initPageData() {
        $.ajax({
            type: 'get',
            dataType: 'json',
            async: false,
            url: '<%=path%>/project/orgBpLimit/initOrgBpCfgPage.do',
            success: function (result) {
                if (result.code == 000000) {
                    $("#bpPct").text(result.data.bpPct);
                    $("#limitPct").text(result.data.limitPct);
                    $("#uncheckCntspn").text(result.data.uncheckCnt);
                    initLogGrid(result.data.logList);
                } else {
                    top.$.ligerDialog.SQalert({content: "获取数据失败！", SQ_type: "warning"});
                }
            },
            error: function () {
                top.$.ligerDialog.SQalert({content: "获取数据失败！", SQ_type: "warning"});
            }
        });
    }

    function initLogGrid(editLogList) {
        editLogList = JSON.parse(editLogList);
        var liger_route_result = $("#ligergridLog").ligerGrid({
            data: getRows(editLogList),
            dataAction: 'local',
            columns: [
                {
                    display: '更新时间', name: 'crtTm', minWidth: 50,
                    editable:false,
                    editor: {
                        type: 'date',
                        readonly: true
                    },
                    render: function (item) {
                        var date = new Date(item.crtTm);
                        if (date) {
                            return DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
                        } else {
                            return '';
                        }
                    }
                },
                {
                    display: '处理人', name: 'userName', minWidth: 50,
                    editable: false,
                    render: function (item) {
                        if (item.userName && item.userNo) {
                            return item.userName + '-' + item.userNo;
                        } else {
                            return '系统';
                        }
                    }
                },

                {
                    display: '变更前台数', name: 'originVal', minWidth: 50,
                    editable: false
                },
                {
                    display: '变更后台数', name: 'newVal', minWidth: 50,
                    editable: false
                }
            ],
            // root: 'rows',
            // record: 'total',
            // hideLoadButton: true,
            width: '60%',
            height: '100%',
            heightDiff: -2,
            enabledEdit: true,
            isScroll: false,
            checkbox: false,
            frozen: false,
            rownumbers: true,
            allowUnSelectRow: true,
            alternatingRow: false,
            // enabledSort: false, // 增加行的时候，出现问题，所以不过滤了
            fixedCellHeight: false,
            usePager: true,
            onAfterShowData: function () {
                SetIframe_height();
            }
        });
    }

    var initProList = function () {
        var proList;
        var liger_route_result = $("#ligergridPro").ligerGrid({
            url: '<%=path%>/project/orgBpLimit/searchBpCfgForOrgList.do',
            method: 'get',
            columns: [
                {
                    display: '事业部', name: 'buOrg', minWidth: 150,
                    editable: false,
                    render: function (item) {
                        if(item.orgCd == '0000000100250001'){
                            return '改装车事业部'
                        }
                        return '重卡事业部';
                    }
                },
                {
                    display: '省公司', name: 'orgNm', minWidth: 150,
                    editable: false,
                    render: function (item) {
                        return item.orgNm;
                    }
                },
                {
                    display: '当月包牌业务支付表数', name: 'bpCount', minWidth: 150,
                    editable: false
                },
                {
                    display: '当月已放款支付表数', name: 'allCount', minWidth: 150,
                    editable: false
                },
                {
                    display: '当前比例', name: 'bpPct', minWidth: 150,
                    editable: false,
                    render: function (item) {
                        if (item.allCount) {
                            var bpPct = (item.bpCount * 100 / item.allCount).toFixed(2);
                            if (bpPct >= item.limitVal * 0.50) {
                                return "<span style='color:red'>" + bpPct + "%</span>";
                            } else {
                                return bpPct + "%";
                            }
                        }else if(item.limitVal === 0){
                            return "<span style='color:red'>0%</span>";
                        }else{
                            return "0";
                        }
                    }
                },
                {
                    display: '额度比例上限', name: 'limitVal', minWidth: 100,
                    editable: false,
                    render: function (item) {
                        return item.limitVal + '%';
                    }
                },
                {
                    display: '操作', name: '', minWidth: 100,
                    editable: false,
                    render: function (item) {
                        <sys:funcAuth resCd="bpCfgModify">
                            return '<a class="sq-button-hasicon" href="javascript:modifyProBtn(\'' + item.orgCd + '\',\'' + item.orgNm + '\',\'' + item.limitVal + '\')" ><i class="sqBtnIco_4"></i>修改</a>';
                        </sys:funcAuth>
                    }
                }
            ],
            root: 'data',
            // record: 'total',
            // hideLoadButton: true,
            width: '100%',
            height: '100%',
            heightDiff: -2,
            enabledEdit: true,
            isScroll: false,
            checkbox: false,
            frozen: false,
            rownumbers: true,
            allowUnSelectRow: true,
            alternatingRow: false,
            // enabledSort: false, // 增加行的时候，出现问题，所以不过滤了
            fixedCellHeight: false,
            usePager: false,
            onAfterShowData: function () {
                SetIframe_height();
            }
        });
    }

    function getRows(dataList) {
        var data = {Rows: [], total: 0};
        if (dataList === undefined) {
            return data;
        }
        for (var i = 0; i < dataList.length; i++) {
            data.Rows.push(dataList[i]);
            data.total++;
        }
        return data;
    }

    function modifyProBtn(orgCd, orgNm, limitVal) {
        $.ligerDialog.prompt(orgNm + '包牌额度比例上限', limitVal, function (yes, value) {
            if (yes) {
                if(value < 0 || value > 100){
                    top.$.ligerDialog.SQalert({content:"请填写0~100之间的数!", SQ_type: "error"});
                    return false ;
                }

                $.ajax({
                    type: 'get',
                    dataType: 'json',
                    async: false,
                    url: '<%=path%>/project/orgBpLimit/updateProLimitVal.do?value=' + value + '&orgCd=' + orgCd,
                    success: function (result) {
                        if (result.code == 000000) {
                            initProList();
                        } else {
                            top.$.ligerDialog.SQalert({content: "修改失败！", SQ_type: "warning"});
                        }
                    },
                    error: function () {
                        top.$.ligerDialog.SQalert({content: "修改失败！", SQ_type: "warning"});
                    }
                });
            } else {

            }
        });
    }

    var initSubList = function () {
        var proList;
        var liger_route_result = $("#ligergridSub").ligerGrid({
            url: '<%=path%>/project/orgBpLimit/getSubBpCfgList.do',
            method: 'get',
            columns: [
                {
                    display: '事业部', name: 'buOrg', minWidth: 150,
                    editable: false,
                    render: function (item) {
                        if (item.orgCd.substring(0,12) == '000000010025'){
                            return '改装车事业部' ;
                        }
                        return '重卡事业部';
                    }
                },
                {
                    display: '省公司', name: 'proNm', minWidth: 150,
                    editable: false,
                    render: function (item) {
                        return item.proNm;
                    }
                },
                {
                    display: '分公司', name: 'orgNm', minWidth: 150,
                    editable: false,
                    render: function (item) {
                        return item.orgNm;
                    }
                },
                {
                    display: '当月包牌业务支付表数', name: 'bpCount', minWidth: 150,
                    editable: false
                },
                {
                    display: '当月已放款支付表数', name: 'allCount', minWidth: 150,
                    editable: false
                },
                {
                    display: '当前包牌业务占比', name: 'bpPct', minWidth: 100,
                    editable: false,
                    render: function (item) {
                        if (item.allCount) {
                            var bpPct = (item.bpCount * 100 / item.allCount).toFixed(2);
                            return bpPct + "%";
                        }
                        return '0';
                    }
                }
            ],
            root: 'data',
            // record: 'total',
            // hideLoadButton: true,
            width: '80%',
            height: '100%',
            heightDiff: -2,
            enabledEdit: true,
            isScroll: false,
            checkbox: false,
            frozen: false,
            rownumbers: true,
            allowUnSelectRow: true,
            alternatingRow: false,
            // enabledSort: false, // 增加行的时候，出现问题，所以不过滤了
            fixedCellHeight: false,
            usePager: false,
            onAfterShowData: function () {
                SetIframe_height();
            }
        });
    }
    $(function () {
        initPageData();
        $(function () {
            $("#pageInTab").ligerTab({
                onAfterSelectTabItem:function(tabid){
                    if(tabid == 'tabA'){
                        initPageData();
                    }
                    if (tabid == 'tabB'){
                        initProList();
                    } else{
                        initSubList() ;
                    }
                }
            });
        });
    })
</script>
</body>
</html>